package com.insyte.utils;

import android.util.Log;

import com.insyte.model.Category;
import com.insyte.model.CategoryList;
import com.insyte.model.Cursors;
import com.insyte.model.FacebookData;
import com.insyte.model.Paging;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public final class ParserFaceBookUntils {
	private static final String TAG = "ParserUtils";

	public static JSONObject getJsonObject(JSONObject obj, String name) {
		JSONObject jsonObject = new JSONObject();
		try {
			String str = obj.getString(name);
			jsonObject = new JSONObject(str);
		} catch (JSONException e) {
			Log.d(TAG, "JSONObject width" + name + " is null");
		}
		return jsonObject;
	}

	public static JSONArray getJsonArray(JSONObject obj, String name) {
		JSONArray jsonArray = new JSONArray();
		try {
			jsonArray = obj.getJSONArray(name);
		} catch (JSONException e) {
			Log.d(TAG, "array width" + name + " is null");
		}
		return jsonArray;
	}

	public static String getStringValue(JSONObject obj, String key) {
		try {
			return obj.isNull(key) ? "" : obj.getString(key);
		} catch (JSONException e) {
			return "";
		}
	}

	public static HashMap<String, String> getBookingIdAndTime(JSONObject json) {
		HashMap<String, String> mapData = new HashMap<String, String>();
		try {
			JSONObject jsonObject = json.getJSONObject("Item");
			if (jsonObject.has("Id"))
				mapData.put("Id", jsonObject.getString("Id"));
			if (jsonObject.has("ThoiGian"))
				mapData.put("ThoiGian", jsonObject.getString("ThoiGian"));
		} catch (Exception e) {
		}
		return mapData;
	}
	public  static FacebookData facebookData (JSONObject json) {
		FacebookData iData=new FacebookData();
		iData.setCategories(getCategories(json));
		iData.setPaging(getPaging(json));
		return iData;
	}
	public static ArrayList<Category> getCategories(JSONObject json) {
		ArrayList<Category> lists = new ArrayList<Category>();
		try {
			JSONArray arrItem = getJsonArray(json, "data");
			for (int i = 0; i < arrItem.length(); i++) {
				JSONObject objItem = arrItem.getJSONObject(i);
				Category item = new Category();
				item.setId(getStringValue(objItem, "id"));
				item.setName(getStringValue(objItem, "name"));
				item.setCategory(getStringValue(objItem, "category"));
				item.setCreated_time(getStringValue(objItem, "created_time"));
				JSONArray arrCategoryList = getJsonArray(objItem, "category_list");
				item.setCategoryLists(getCategoryLists(arrCategoryList));
				lists.add(item);
			}
		} catch (Exception e) {
		}
		return lists;
	}
	public static ArrayList<CategoryList> getCategoryLists(JSONArray arrItem) {
		ArrayList<CategoryList> lists = new ArrayList<CategoryList>();
		try {

			for (int i = 0; i < arrItem.length(); i++) {
				JSONObject objItem = arrItem.getJSONObject(i);
				CategoryList item = new CategoryList();
				item.setId(getStringValue(objItem, "id"));
				item.setName(getStringValue(objItem, "name"));
				lists.add(item);
			}
		} catch (Exception e) {
		}
		return lists;
	}

	public static Paging getPaging(JSONObject json) {
		Paging item = new Paging();
		try {
			JSONObject jsonObject = getJsonObject(json, "paging");
			item.setNext(getStringValue(jsonObject, "next"));
			item.setCursors(getCursors(getJsonObject(jsonObject,"cursors")));
		} catch (Exception e) {
		}
		return item;
	}
	public static Cursors getCursors(JSONObject json) {
		Cursors item = new Cursors();
		try {
			item.setBefore(getStringValue(json, "before"));
			item.setAfter(getStringValue(json, "after"));
		} catch (Exception e) {
		}
		return item;
	}

	/**
	 * Get long value
	 * 
	 * @param obj
	 * @param key
	 * @return
	 */
	public static long getLongValue(JSONObject obj, String key) {
		try {
			return obj.isNull(key) ? 0L : obj.getLong(key);
		} catch (JSONException e) {
			return 0L;
		}
	}

	public static int getIntValue(JSONObject obj, String key) {
		try {
			return obj.isNull(key) ? 0 : obj.getInt(key);
		} catch (JSONException e) {
			return 0;
		}
	}

    public static int getIntValueResponse(JSONObject obj, String key) {
        try {
            return obj.isNull(key) ? -1 : obj.getInt(key);
        } catch (JSONException e) {
            return -1;
        }
    }

	public static Double getDoubleValue(JSONObject obj, String key) {
		double d = 0.0;
		try {
			d = obj.isNull(key) ? d : obj.getDouble(key);
			return d;
		} catch (JSONException e) {
			return d;
		}
	}

	public static boolean getBooleanValue(JSONObject obj, String key) {
		try {
			return obj.isNull(key) ? false : obj.getBoolean(key);
		} catch (JSONException e) {
			return false;
		}
	}

	@SuppressWarnings("deprecation")
	public static String JsonDateToDate(String jsonDate) {
		// "/Date(1321867151710)/"
		int idx1 = jsonDate.indexOf("(");
		int idx2 = jsonDate.indexOf(")");
		String s = jsonDate.substring(idx1 + 1, idx2);
		long l = Long.valueOf(s);
		Date result = new Date(l);
		int Year = result.getYear() + 1900;
		return result.getDate() + "/" + result.getMonth() + "/" + Year;
	}
}